__author__ = 'zoulida'

import datetime
import time

def dayStrToTimeStamp(dayStr = "20190521"):
    timeArray = time.strptime(dayStr, "%Y%m%d")
    timeStamp = int(time.mktime(timeArray))
    return timeStamp

def getDayStr(days):#返回当天日期与days天之前的日期，str类型

    today = datetime.date.today()
    z30daysago = today + datetime.timedelta(days=-days)
    #startstr = '19900101'
    startstr = str(z30daysago.strftime('%Y%m%d'))
    endstr = str(today.strftime('%Y%m%d'))
    return startstr, endstr

def getDayList(startDay, endDay):#返回日期list
    return

def dateRange(beginDate, endDate):#返回日期list
    dates = []
    dt = datetime.datetime.strptime(beginDate, "%Y%m%d")
    date = beginDate[:]
    while date <= endDate:
        dates.append(date)
        dt = dt + datetime.timedelta(1)
        date = dt.strftime("%Y%m%d")
    return dates

def spanTime():
    start = datetime.datetime.now()
    time.sleep(5)
    end = datetime.datetime.now()

    print((end - start).seconds)






def ttttt():
    # encoding: utf-8
    '''
    @author: weiyang_tang
    @contact: weiyang_tang@126.com
    @file: datetime_timeStamp_str.py
    @time: 2019-02-06 11:44
    @desc: 时间日期、时间戳、字符串互转、日期差
    '''

    import time
    import datetime
    # 获取当前日期
    now_dateTime = datetime.datetime.now()  # 获取当前日期 格式：2019-02-06 11:47:36.406318
    print("获取当前日期:", now_dateTime)

    # 将datetime类型转成str类型
    # strftime("%Y-%m-%d %H:%M:%S") datetime->str
    now_str = now_dateTime.strftime("%Y-%m-%d %H:%M:%S")
    print("将datetime类型->str类型", now_str)

    # str->dateTime 字符串转成日期datetime
    # strptime(str,format)
    oneDay_str = "2019-2-6 12:1:0"
    oneDay_datetime = datetime.datetime.strptime(oneDay_str, "%Y-%m-%d %H:%M:%S")
    print("字符串转成日期datetime", oneDay_datetime)

    # 获取当前时间时间戳
    now_timestamp = time.time()
    print("获取当前时间的时间戳", now_timestamp)

    # 字符串转成时间戳str->timestamp
    oneDay_str = "2019-2-6 12:1:0"
    oneDay_timestamp = time.mktime(time.strptime(oneDay_str, "%Y-%m-%d %H:%M:%S"))
    print("字符串转时间戳:", oneDay_timestamp)

    # 时间戳转成日期 timestamp->datetime
    # now_dateTime=datetime.datetime.strptime()
    now_dateTime = datetime.datetime.fromtimestamp(now_timestamp)
    print("时间戳转成日期:", now_dateTime)

    # 两个日期差
    oneDay_str = "2019-2-6 20:30:0"
    now_timestamp = time.time()
    oneDay_timestamp = time.mktime(time.strptime(oneDay_str, "%Y-%m-%d %H:%M:%S"))
    difTime = now_timestamp - oneDay_timestamp
    print("时间间隔为:", difTime, "秒")

def ttt2():
    import datetime
    import time

    d1 = datetime.datetime(2005, 2, 16)
    d2 = datetime.datetime(2004, 12, 31)
    tdelta = d1 - d2
    print(tdelta.seconds, tdelta.days)

    starttime = datetime.datetime.now()
    time.sleep(10)
    endtime = datetime.datetime.now()
    print((endtime - starttime).seconds)

    d1 = datetime.datetime.now()
    d2 = d1 + datetime.timedelta(hours=10)
    print(        d2.ctime())

if __name__ == '__main__':
    #print(dayStrToTimeStamp())
    #print(dateRange('20160301', '20190522'))
    ttt2()